package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.SysModuleMapper;
import com.qf.agro.pojo.SysModule;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ProjectName: smart-agriculture-parent
 * ClassName: SysModuleServiceImpl
 * Author: chenyaosen
 * Date: 2023/7/3 21:52
 * Description:
 */
@Service
public class SysModuleServiceImpl implements SysModuleService  {

    @Autowired
    private SysModuleMapper moduleMapper;

    @Override
    public List<Long> findRoleModule(Long roleId) {
        List<Long> list = moduleMapper.selectRoleModule(roleId);
        return list;
    }

    @Override
    public Page<SysModule> findPage(Integer pageNum, Integer pageSize, String name) {
        Page<SysModule> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, SysModule::getName,name);
        page = moduleMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public List<SysModule> findAll() {
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(SysModule::getParentName);
        List<SysModule> parent = moduleMapper.selectList(queryWrapper);
        for (SysModule sysModule : parent) {
            LambdaQueryWrapper<SysModule> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysModule::getParentId, sysModule.getId());
            wrapper.isNull(SysModule::getCtype);
            List<SysModule> children = moduleMapper.selectList(wrapper);
            sysModule.setChildren(children);
        }
        return parent;
    }

    @Override
    public void updateRoleModule(Long roleId, List<Long> moduleIds) {
        moduleMapper.selectRoleModule(roleId);
        moduleMapper.insertRoleModule(roleId, moduleIds);
    }
}
